#include <iostream>
#include <gtest/gtest.h>
#include "melon/client/thrift_client_pool.h"
#include "base/logging.h"
#include "PeAskNotifierService.h"
#include "phone_expert/adapters/ask_notifier_adapter.h"

using namespace pe;
using namespace std;

const int repeat_times = 100000;
const int kThreadCount = 5;

void * ThreadFun(void * data) {
  int max_retry = 3; 
  for(int i=0; i<repeat_times; ++i){
    bool success = true;
    int64_t qihu_id = 848981653;
    int64_t answerer_id = 222;
    int64_t question_id = 333;
    int32_t count = 0;
    int32_t delta = 45;
    try {

//    AddUserAndQuestion(const int64_t qihu_id, const int64_t question_id)
//    AskNotifierAdapter::Instance().AddUserAndQuestion(qihu_id, question_id, max_retry);
//    LOG_DEBUG("PeAskNotifierServiceTest AddUserAndQuestion : " << qihu_id << " " << question_id);

      count = AskNotifierAdapter::Instance().GetAnswerCount(answerer_id, question_id, max_retry);
      LOG_DEBUG("PeAskNotifierServiceTest GetAnswerCount : " << answerer_id << " " << question_id << " : " << count);

      ++count;
      // NotifyAnswer(const int64_t qihu_id, const int64_t question_id, const int64_t answerer_id, const int32_t delta)
      AskNotifierAdapter::Instance().NotifyAnswer(qihu_id, question_id, answerer_id, count, true, max_retry);
      LOG_DEBUG("PeAskNotifierServiceTest NotifyAnswer : " << qihu_id << " " << question_id << " " << answerer_id << " " << count);

      // SetAnswerCount(const int64_t qihu_id, const int64_t question_id, const int32_t count)
      AskNotifierAdapter::Instance().SetAnswerCount(answerer_id, question_id, count, max_retry);
      LOG_DEBUG("PeAskNotifierServiceTest SetAnswerCount : " << answerer_id << " " << question_id << " : " << count);

      count = AskNotifierAdapter::Instance().GetAnswerCount(answerer_id, question_id, max_retry);
      LOG_DEBUG("PeAskNotifierServiceTest GetAnswerCount : " << answerer_id << " " << question_id << " : " << count);

    } catch (const std::exception & e) {
      std::cout << " error:" << e.what() << std::endl;
      success = false;
    }
    
  }

  return NULL;
}

int main() {
  LOG_INIT("./log/test.log", "DEBUG");

  pthread_t ts[kThreadCount];

  for (int i = 0; i < kThreadCount; ++i) {
    pthread_create(&ts[i], NULL, ThreadFun, NULL);
  }

  for (int i = 0; i < kThreadCount; ++i) {
    pthread_join(ts[i], NULL);
  }

  return 0;
}
